define('view/sys/user', ['WxCore',"vuex", 'text!./user.html'], function (wxCore,{ mapActions }, template) {

    return {
        template: template,
        data: function () {
            return {
                totalCount: 0,
                pageIndex:1,
                pageSize:10,
                tableData:[],
                columns:[
                    {
                        type: 'index',
                        title: '序号',
                        align: 'center',
                        width: 60
                    },
                    {
                        title: '帐户',
                        key: 'account'
                    },
                    {
                        title: '姓名',
                        key: 'userName'
                    },
                    {
                        title: '用户类型',
                        key: 'userType',
                        render: (h, params) => {
                            let userTypeName = '管理员'
                            if (params.row.userType === '1') {
                                userTypeName = '普通用户'
                            }
                            return h('div', [
                                h(
                                    'span',
                                    {
                                        style: {
                                            marginRight: '5px',
                                            marginLeft: '5px'
                                        }
                                    },
                                    userTypeName
                                )
                            ])
                        }
                    },
                    {
                        title: '状态',
                        key: 'userLock',
                        render: (h, params) => {
                            let userLockName = '正常'
                            if (params.row.userLock === '1') {
                                userLockName = '锁定'
                            }
                            return h('div', [
                                h(
                                    'span',
                                    {
                                        style: {
                                            marginRight: '5px',
                                            marginLeft: '5px'
                                        }
                                    },
                                    userLockName
                                )
                            ])
                        }
                    },
                    {
                        title: '操作',
                        key: this.columnKey,
                        align: 'center',
                        fixed: 'right',
                        width: 200,
                        render: (h, params) => {
                            return h('div', [
                                h(
                                    'Button',
                                    {
                                        props: {
                                            type: 'default',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.editAction(params)
                                            }
                                        }
                                    },
                                    '编辑'
                                ),
                                h(
                                    'Button',
                                    {
                                        props: {
                                            type: 'default',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.deleteAction(params)
                                            }
                                        }
                                    },
                                    '删除'
                                )
                            ])
                        }
                    }
                ],
                loading:false,
                stripe:false,
                modalTitle:'',
                modalShow:false,
                rolesLoaded:false,
                formRef: 'editForm',
                formData: {},
                formValidateRule: {},
                roles: [],
                rolesLoaded: false
            }
        },
        methods: {
            ...mapActions('UserStore', [
                'getValidateRule',
                'list',
                'find',
                'save',
                'delete'
            ]),
            ...mapActions('UserRoleStore', [
                'listRoleAll'
            ]),
            onChangePage: function (currentPageIndex) {
                this.pageIndex = currentPageIndex;
                this.loadData();
            },
            loadRoles: function () {
                if (!this.rolesLoaded) {
                    this.listRoleAll().then((responseResult) => {
                        //this.$Message.info('用户角色列表加载成功');
                        this.roles = responseResult.data;
                        this.rolesLoaded = true;
                    }).catch(err => {
                        this.$Message.error('用户角色列表加载失败');
                    });
                }
            },
            loadData: function() {
                this.loading=true;
                this.list({
                    pageIndex:this.pageIndex,
                    pageSize:this.pageSize
                }).then((responseResult) => {
                    this.totalCount = responseResult.total;
                    this.tableData = responseResult.list.map((item, index) => {
                        let itemMap = wxCore.propertieToString(
                            item
                        )
                        return itemMap
                    })
                    this.loading = false;
                }).catch(err => {
                    console.log(err)
                    this.loading = false
                });
                //this.$emit('load-data', pageInfo)
            },
            // 打开新增窗口
            addAction: function () {
                this.clearFormData()
                this.loadRoles()
                this.formData = {
                    sex: '1',
                    email: 'wyy@qq.com',
                    userType: '1'
                }
                //console.log('showAdd')
                this.modalTitle = '新增'
                this.modalShow = true
            },
            // 刷新列表
            refreshAction: function () {
                this.loadData();
            },
            formCancelHandle: function () {
                this.modalShow = false
            },
            formSaveHandle: function () {
                this.$refs[this.formRef].validate(valid => {
                    if (!valid) {
                        this.$Message.error('输入信息不合法')
                    } else {
                        //console.log(this.formData);
                        this.save(this.formData)
                            .then(res => {
                                this.clearFormData();
                                this.modalShow = false;
                                this.$Message.info('保存成功');
                                this.loadData();
                            })
                            .catch(err => {
                                this.$Message.error('登录帐户已经存在或服务异常');
                                console.log('>>>err:', err);
                            })
                    }
                });
            },
            clearFormData: function () {
                this.$refs[this.formRef].resetFields();
                this.formData = {};
            },
            editAction (params) {
                this.clearFormData();
                this.loadRoles();
                this.modalTitle = '编辑';

                this.find(params.row.userId).then((responseResult) => {
                    this.formData = wxCore.propertieToString(
                        responseResult
                    );
                    this.modalShow = true;
                }).catch(err => {
                    this.$Message.error('用户信息加载失败');
                })
            },
            deleteAction (params) {
                this.$Modal.confirm({
                    title: '删除',
                    content: '确定要删除么？',
                    onOk: () => {
                        this.delete(params.row.userId)
                            .then(res => {
                                this.$Message.info('删除成功')
                                this.loadData()
                            })
                            .catch(err => {
                                this.$Message.error(err)
                            })
                    }
                })

            }
        },
        created() {
            // 加载验证规则
            this.getValidateRule().then(val => {
                this.formValidateRule = {}
                for (let ruleColumn in val) {
                    this.formValidateRule[ruleColumn] = val[ruleColumn]
                }
            });
            this.loadData();
        }
    }
})